<template>
  <div class='container'>
    <el-form
    :model="ruleForm"
     :rules="rules"
      ref="ruleForm"
       label-width="80px">
 <el-form-item label="文章标题" prop="title">
    <el-input v-model="ruleForm.title" placeholder="请输入文章标签"></el-input>
  </el-form-item>
    <el-form-item label="文章内容" prop="articleBody">
        <!-- bidirectional data binding（双向数据绑定） -->
  <quill-editor @change="$refs.ruleForm.clearValidate('articleBody')" v-model="ruleForm.articleBody" :options="editorOption"></quill-editor>
    </el-form-item>
     <el-form-item label="视频地址" >
    <el-input v-model="ruleForm.videoURL" placeholder="请输入视频地址"></el-input>
  </el-form-item>
   <el-form-item style="float:right;padding-top:30px" >
        <el-button @click="succeed()">取 消</el-button>
    <el-button type="primary" @click="submitForm(isEdit)">确 定</el-button>
  </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { add, detail, update } from '@/api/hmmm/articles'
export default {
  props: {
    isEdit: {
      type: Boolean,
      required: true
    },
    id: {
      type: Number,
      default: null
    }
  },
  data () {
    return {
      ruleForm: {
        id: null,
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章名称', trigger: 'blur' }
        ],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      },
      // delivery: true,
      options: [{
        value: '1',
        label: ' 已启用 '
      }, {
        value: '0',
        label: ' 已禁用 '
      }],
      value: '',
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线

            // [{ header: 1 }, { header: 2 }], // 标题，键值对的形式；1、2表示字体大小
            [{ list: 'ordered' }, { list: 'bullet' }], // 列表
            ['blockquote', 'code-block'], // 引用，代码块
            // [{ script: 'sub' }, { script: 'super' }], // 上下标
            // [{ indent: '-1' }, { indent: '+1' }], // 缩进
            // [{ direction: 'rtl' }], // 文本方向

            // [{ size: ['small', false, 'large', 'huge'] }], // 字体大小
            // [{ header: [1, 2, 3, 4, 5, 6, false] }], // 几级标题

            // [{ color: [] }, { background: [] }], // 字体颜色，字体背景颜色
            // [{ font: [] }], // 字体
            // [{ align: [] }], // 对齐方式

            // ['clean'], // 清除字体样式
            ['image', 'video'] // 上传图片、上传视频
          ]
        }
      }
    }
  },
  async created () {
    // console.log(this.subjectID)
    // if(this.subjectId)
    // this.getsimple()
    // console.log('this.id', this.id)
    if (this.isEdit) {
      this.ruleForm.id = this.id
      const { data: res } = await detail({ id: this.ruleForm.id })
      // console.log(res)
      this.ruleForm = res
      // this.value = res.subjectID
    }
  },
  methods: {
    // // 获取学科所属
    // async getsimple () {
    //   const res = await simple()
    //   // console.log(res)
    //   this.options = res.data
    // },
    submitForm () {
      // 兜底校验
      this.$refs.ruleForm.validate(reslut => {
        // console.log('reslut', reslut)
        if (!reslut) return
        // if (this.delivery) {
        //   this.ruleForm.isFrontDisplay = 1
        // } else {
        //   this.ruleForm.isFrontDisplay = 0
        // }
        if (this.subjectID) {
          this.ruleForm.subjectID = this.subjectID
        } else {
          this.ruleForm.subjectID = this.value
        }
        if (!this.isEdit) {
          this.addSub()
        } else {
          this.delSub()
        }
      })
    },
    addSub () {
      // console.log('this.ruleForm.isFrontDisplay', this.ruleForm.isFrontDisplay)
      add(this.ruleForm).then(response => {
        // console.log('1234', response)
        this.$message.success('新增文章成功!')
        this.succeed()
      }).catch(response => {
        this.$message.error('新增失败!')
      })
      // this.$message.info('已取消操作!')
    },
    delSub () {
      update(this.ruleForm).then(response => {
        // console.log('1234', response)
        this.$message.success('修改文章成功!')
        this.succeed()
      }).catch(response => {
        this.$message.error('修改失败!')
      })
    },
    succeed () {
      this.ruleForm = {
        id: null,
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$emit('success')
    }

  }
}
</script>

<style scoped lang='scss'>
  .container {
    height: 380px;
  ::v-deep .ql-container {
  .ql-editor {
      min-height: 130px
    }
   }
  }
</style>
